package com.st.buy.sso.controller;

import com.st.buy.pojo.TbUser;
import com.st.buy.sso.service.UserService;
import com.st.buy.util.pojo.BuyResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

/**
 * @author 张双虎
 */
@RestController
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("/user/checktoken/{token}")
    public BuyResult checkUser(@PathVariable String token){
        return userService.checkToken(token);
    }

    @RequestMapping("/user/login")
    public BuyResult login(String username, String password, HttpServletResponse response){
        BuyResult result = userService.login(username,password);
        //判断登录是否成功
        if(result.getStatus()==200){
            //登录成功
            //取出token
            String token = result.getData().toString();
            //生成cookie
            Cookie cookie = new Cookie("TOKNE",token);// 理论上都是要加密的
            //设置cookie的信息
            //设置路径
            cookie.setPath("/");
            //设置cookie的域    www.buy.com  sso.buy.com  search.buy.com  item.buy.com  cart.buy.com   order.buy.com   manager.ebuy.com
//            cookie.setDomain(".buy.com"); // 我们目前的域名都是loclhost，没有跨域问题
            response.addCookie(cookie);
        }
        return result;
    }

    @RequestMapping("/user/register")
    public BuyResult regist(TbUser user){
        return userService.regist(user);
    }

    @RequestMapping("/user/check/{data}/{type}")
    public BuyResult checkData(@PathVariable String data,@PathVariable int type){
        return userService.check(data,type);
    }
}
